package com.wang.auth.controller;


import com.wang.auth.service.SysMenuService;
import com.wang.common.result.Result;
import com.wang.model.system.SysMenu;
import com.wang.model.system.vo.AssignMenuVo;
import com.wang.model.system.vo.AssignRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "获取菜单")
    @GetMapping("findNodes")
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    public Result findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.success(list);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    public Result save(@RequestBody SysMenu permission) {
        sysMenuService.save(permission);
        return Result.success();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    public Result updateById(@RequestBody SysMenu permission) {
        sysMenuService.updateById(permission);
        return Result.success();
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    public Result remove(@PathVariable Long id) {
        sysMenuService.removeMenuById(id);
        return Result.success();
    }

    @ApiOperation(value = "根据角色id获取菜单")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId){
        return Result.success(sysMenuService.findSysMenuByRoleId(roleId));
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    public Result doAssign(@RequestBody AssignMenuVo assignMenuVo){
        return Result.success(sysMenuService.doAssign(assignMenuVo) ? "分配成功" : "分配失败");
    }

}
